<?php

namespace App\Http\Controllers\Article;

use App\Column;
use App\Http\Request\CreateArticleRequest;
use App\Http\Request\GetColumnByIdRequest;
use App\Http\Request\GetTagsByColumnIdRequest;
use App\Http\Request\SearchForArticleRequest;
use App\Post;
use App\Post_tag;
use App\Tag;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{

    /**获取所有符合条件的文章
     * @param Request $request 条件
     * @return \Illuminate\Http\JsonResponse
     */
    public function searchForArticles(SearchForArticleRequest $request)
    {
        $title = $request->input('titleName');

        $result = (new Post())->select('id', 'title', 'user_id', 'view', 'like', 'updated_at')
            ->where('title', 'like', '%' . $title . '%')
            ->where('status', '0')->get()->toarray();
        if ($result) {
            $length = sizeof($result);
            $data = null;
            for ($i = 0; $i < $length; $i++) {
                $username = (new User())->select('user_name')->where('id', $result[$i]['user_id'])->first();

                $data[$i] = [
                    'id'         => $result[$i]['id'],
                    'title'      => $result[$i]['title'],
                    'username'   => $username,
                    'view'       => $result[$i]['view'],
                    'like'       => $result[$i]['like'],
                    'updated_at' => $result[$i]['updated_at'],
                ];

            }
            $reason = [
                'message' => '成功',
                'code'    => 200,
                'data'    => $data
            ];

            return response()->json($reason);
        } else {
            $reason = [
                'message' => '失败',
                'code'    => 100,
                'data'    => null
            ];
            return response()->json($reason);
        }
    }

    /**
     * 获取所有专栏名称
     */
    public function getColumns(){
        $data = (new Column())::all();
        return response()->json($data);
    }

    /**根据专栏Id获取专栏
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getColumnById(GetColumnByIdRequest $request){

        $data = (new Column())->where('id',$request->input('columnId'))->get();
        return response()->json($data);

    }

    /**获取专栏Id对应的所有标签
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getTagsByColumnId(GetTagsByColumnIdRequest $request){
        $result = (new Tag())->where('column_id',$request->input('columnId'))->get();
        if($result){
            $data = [
                'message'=>'成功',
                'code'=>'200',
                'data'=> $result
            ];
            return response()->json($data,200);
        }else{
            $data = [
                'message'=>'失败',
                'code'=>'100',
                'data'=>null
            ];

            return response()->json($data,100);
        }
    }

    /**新建文章
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function createArticle(CreateArticleRequest $request){


        $data = json_decode($request->getContent(),true);



        $post = new Post();
        $post->title = $data['title'];
        $post->body = $data['body'];
        $post->user_id = $data['userId'];
        $post->column_id = $data['columnId'];
        $post->view = 0;
        $post->like = 0;
        $post->hot = 0;
        $post->status = 1;
        $post->img_path = $data['img_path'];

        $result = $post->save();


        for($i = 0;$i<sizeof($data['tags']);$i++){
            $post_tags = new Post_tag();
            $post_tags->post_id = $post->id;
            $post_tags->tag_id = $data['tags'][$i];

            $post_tags->save();

        }


        if($result){
            $data = [
                'message'=>'成功',
                'code'=>'200',
                'data'=> null
            ];
            return response()->json($data,200);
        }else{
            $data = [
                'message'=>'失败',
                'code'=>'100',
                'data'=>null
            ];

            return response()->json($data,100);
        }
    }



}
